package com.tsd.jxc.buy.entity;

import com.tsd.core.constants.Constants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.fin.FinanceConstants;
import com.tsd.jxc.fin.entity.JxcFinanceAccountDetailExt;
import com.tsd.jxc.stock.StockConstants;
import com.tsd.jxc.stock.entity.JxcStockOutbillExt;
import com.tsd.jxc.stock.entity.JxcStockOutbillItemExt;
import com.tsd.jxc.vo.PrintVo;
import com.tsd.system.entity.SysUserExt;
import io.swagger.annotations.ApiModel;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 采购退货单扩展
 *
 * @author Hillpool
 * @date 2022/07/29 15:00:45
 */
@ApiModel(description = "采购退货单扩展")
public class JxcBuyReturnBillExt extends JxcBuyReturnBill implements Serializable {
    /**
     * 暂存
     */
    public static final int STATE_TEMP_SAVE = -1;
    /**
     * 未审核
     */
    public static final int STATE_UN_AUDIT = 0;
    /**
     * 已审核
     */
    public static final int STATE_AUDITED = 1;
    /**
     * 是否生成了出库单 0-否
     */
    public static final int OUT_BILL_NO = 0;
    /**
     * 是否生成了出库单 1-部分生成
     */
    public static final int OUT_BILL_SECTION = 1;
    /**
     * 是否生成了出库单 2-全部生成
     */
    public static final int OUT_BILL_ALL = 2;
    /**
     * 是否生成对账 0-否
     */
    public static final int GEN_FINANCE_NO = 0;
    /**
     * 是否生成对账 1-是
     */
    public static final int GEN_FINANCE_YES = 1;

    private String review_person_name;

    private String supplier_code;

    private String supplier_short_name;

    private String supplier_name_join;

    private String supplier_display;

    private String goods_code;

    private String goods_name;

    private Integer goods_qty;

    private BigDecimal goods_price;

    private BigDecimal goods_amount;

    private String specifications;

    private List<JxcBuyReturnBillDetailExt> detail_list;

    private List<String> sid_list;

    public String getReview_person_name() {
        return review_person_name;
    }

    public void setReview_person_name(String review_person_name) {
        this.review_person_name = review_person_name;
    }

    public String getSupplier_code() {
        return supplier_code;
    }

    public void setSupplier_code(String supplier_code) {
        this.supplier_code = supplier_code;
    }

    public String getSupplier_short_name() {
        return supplier_short_name;
    }

    public void setSupplier_short_name(String supplier_short_name) {
        this.supplier_short_name = supplier_short_name;
    }

    public String getSupplier_name_join() {
        return supplier_name_join;
    }

    public void setSupplier_name_join(String supplier_name_join) {
        this.supplier_name_join = supplier_name_join;
    }

    public String getSupplier_display() {
        return supplier_display;
    }

    public void setSupplier_display(String supplier_display) {
        this.supplier_display = supplier_display;
    }

    public String getGoods_code() {
        return goods_code;
    }

    public void setGoods_code(String goods_code) {
        this.goods_code = goods_code;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public Integer getGoods_qty() {
        return goods_qty;
    }

    public void setGoods_qty(Integer goods_qty) {
        this.goods_qty = goods_qty;
    }

    public BigDecimal getGoods_price() {
        return goods_price;
    }

    public void setGoods_price(BigDecimal goods_price) {
        this.goods_price = goods_price;
    }

    public BigDecimal getGoods_amount() {
        return goods_amount;
    }

    public void setGoods_amount(BigDecimal goods_amount) {
        this.goods_amount = goods_amount;
    }

    public String getSpecifications() {
        return specifications;
    }

    public void setSpecifications(String specifications) {
        this.specifications = specifications;
    }

    public List<JxcBuyReturnBillDetailExt> getDetail_list() {
        return detail_list;
    }

    public void setDetail_list(List<JxcBuyReturnBillDetailExt> detail_list) {
        this.detail_list = detail_list;
    }

    public List<String> getSid_list() {
        return sid_list;
    }

    public void setSid_list(List<String> sid_list) {
        this.sid_list = sid_list;
    }

    public JxcStockOutbillExt genOutBill(String  wareHouse_sid) {
        JxcStockOutbillExt outBillExt = new JxcStockOutbillExt();
        outBillExt.setWarehouse_sid(wareHouse_sid);
        outBillExt.setOut_type(StockConstants.OUT_TYPE_BUY_RETURN);
        outBillExt.setSupplier_sid(this.getSupplier_sid());
        outBillExt.setOut_date(new Date());
        outBillExt.setSrc_sid(this.getSid());
        outBillExt.setSrc_code(this.getCode());
        outBillExt.setExecute_state(Constants.DR_VALID);
        outBillExt.setAudit_state(Constants.DR_VALID);
        List<JxcBuyReturnBillDetailExt> detailExtList = ListUtil.filter(this.getDetail_list(), t -> !StockConstants.DETAIL_IN_OUT_BILL_ALL.equals(t.getOut_bill()));
        List<JxcStockOutbillItemExt> itemExtList = ListUtil.map(detailExtList, t -> t.genOutBillItem());
        outBillExt.setItem_list(itemExtList);
        return outBillExt;
    }

    public List<JxcFinanceAccountDetailExt> genAccountDetail() {
        List<JxcFinanceAccountDetailExt> list = new ArrayList<>();
        JxcFinanceAccountDetailExt detailExt = new JxcFinanceAccountDetailExt();
        detailExt.setType(FinanceConstants.TYPE_PAYABLE);
        detailExt.setBill_type(FinanceConstants.BILL_TYPE_BUY_RETURN);
        detailExt.setRelated_party_sid(this.getSupplier_sid());
        detailExt.setThe_date(new Date());
        detailExt.setPayable_amount(this.getTotal_amount());
        detailExt.setRelation_sid(this.getSid());
        detailExt.setRelation_code(this.getCode());
        list.add(detailExt);
        if (!HlpUtils.isEmpty(this.getIgnore_amount()) && this.getIgnore_amount().doubleValue() > 0) {
            JxcFinanceAccountDetailExt ignoreDetailExt = new JxcFinanceAccountDetailExt();
            ignoreDetailExt.setType(FinanceConstants.TYPE_PAYABLE);
            ignoreDetailExt.setBill_type(FinanceConstants.BILL_TYPE_BUY_RETURN_IGNORE);
            ignoreDetailExt.setRelated_party_sid(this.getSupplier_sid());
            ignoreDetailExt.setThe_date(new Date());
            ignoreDetailExt.setPayable_amount(this.getIgnore_amount());
            ignoreDetailExt.setRelation_sid(this.getSid());
            ignoreDetailExt.setRelation_code(this.getCode());
            list.add(ignoreDetailExt);
        }
        return list;
    }

    public PrintVo convert2Print(SysUserExt userExt) {
        PrintVo vo = new PrintVo();
        vo.setCode(this.getCode());
        vo.setCreator_name(this.getCreator_name());
        vo.setCreate_date(this.getCreate_date());
        vo.setAuditor_name(this.getAuditor_name());
        vo.setAudit_date(this.getAudit_date());
        vo.setReturn_date(this.getReturn_date());
        vo.setSupplier_display(this.getSupplier_display());
        vo.setSupplier_contract_name(this.getSupplier_contract_name());
        vo.setSupplier_contract_phone(this.getSupplier_contract_phone());
        vo.setQty(this.getQty());
        vo.setAmount(this.getAmount());
        vo.setPre_freight(this.getPre_freight());
        vo.setReal_freight(this.getReal_freight());
        vo.setFree_shipping(this.getFree_shipping());
        vo.setTotal_amount(this.getTotal_amount());
        vo.setIgnore_amount(this.getIgnore_amount());
        vo.setShould_return_amount(this.getShould_return_amount());
        vo.setReturn_amount(this.getReturn_amount());
        vo.setUn_return_amount(this.getUn_return_amount());
        vo.setDelivery_mode(this.getDelivery_mode());
        vo.setState(this.getState());
        vo.setNote_info(this.getNote_info());
        vo.setOut_bill(this.getOut_bill());
        vo.setGen_finance(this.getGen_finance());
        List<PrintVo> detailList = ListUtil.map(this.getDetail_list(), JxcBuyReturnBillDetailExt::convert2Print);
        vo.setDetail_list(detailList);
        if (userExt != null) {
            vo.setPrint_name(userExt.getName());
        }
        vo.setPrint_datetime(new Date());
        return vo;
    }

    public String getState_name() {
        if (!HlpUtils.isEmpty(this.getState())) {
            if (this.getState() == STATE_UN_AUDIT) {
                return "待审核";
            } else if (this.getState() == STATE_AUDITED) {
                return "已审核";
            } else if (this.getState() == STATE_TEMP_SAVE) {
                return "暂存";
            }
        }
        return null;
    }

    public String getOut_bill_name() {
        if (!HlpUtils.isEmpty(this.getOut_bill())) {
            if (this.getOut_bill() == OUT_BILL_NO) {
                return "否";
            } else if (this.getOut_bill() == OUT_BILL_SECTION) {
                return "部分生成";
            } else if (this.getOut_bill() == OUT_BILL_ALL) {
                return "全部生成";
            }
        }
        return null;
    }

    public String getGen_finance_name() {
        if (!HlpUtils.isEmpty(this.getGen_finance())) {
            if (this.getGen_finance() == GEN_FINANCE_NO) {
                return "否";
            } else if (this.getGen_finance() == GEN_FINANCE_YES) {
                return "是";
            }
        }
        return null;
    }

}
